package phoenix.mob_android.page_objects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseMobAndroidExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.mob_android.locators.base.SettingsPageLocatorsBase;

public class SettingsPage extends PageBaseMobAndroidExt
{
	public SettingsPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	private String p_name;
	private WebElement p_winks;

	/**
	 * Gets web elements by site
	 * @return SettingsPageLocatorsBase instance
	 */
	public SettingsPageLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.mob_android.locators.flirt.SettingsPageLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.mob_android.locators.wildbuddies.SettingsPageLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.mob_android.locators.cheeckylovers.SettingsPageLocators();
			}
			default:
			{
				return null;
			}
		}
	}

	public void clickLogoutButton()
	{
		md().waitForPresence(elements().btnLogout, "Logout button doesn't appear");
		md().click(elements().btnLogout);
	}

	public void clickBackAndroidButton()
	{
		manager.md().driver().navigate().back();
	}

	public SettingsPage openBlackList()
	{
		md().waitForPresence(elements().btnMyBlacklist, "Blacklist button doesn't appear");
		md().click(elements().btnMyBlacklist);

		return this;
	}

	public void checkForEmptyBlackList()
	{
		md().waitForPresence(elements().blockEmptyBlackList, "Message of empty block list doesn't appear");
	}

	public String getScreennameInBlackList()
	{

		if (md().isElementPresent(elements().blockUserInBlackListLoadProgress) || md().isElementPresent(elements().blockUserInBlackListLoad))
		{
			return md().getText(elements().blockUserScreennameInBlackList);
		}
		else
		{
			return null;
		}
	}

	public void openProfileFromBlackList()
	{
		if (md().isElementPresent(elements().blockUserInBlackListLoadProgress))
		{
			md().click(elements().blockUserInBlackListLoadProgress);
		}
		else if (md().isElementPresent(elements().blockUserInBlackListLoad))
		{
			md().click(elements().blockUserInBlackListLoad);
		}
	}

	public SettingsPage isOnSettingsPage()
	{
		md().waitForPresence(elements().blockSettings, "Settings page doesn't appear");

		return this;
	}

	public String setScreenname(String p_nameUniquePrefix)
	{
		md().click(elements().btnChangeScreenname);
		md().waitForVisibility(elements().inputFieldChangeScreenname, "Input field change screenname doesn't appear");
		p_name = md().getText(elements().inputFieldChangeScreenname);
		md().typeAndClear(elements().inputFieldChangeScreenname, p_name + p_nameUniquePrefix);
		md().wait(2);
		this.clickBackAndroidButton();
		md().click(elements().btnSubmitScreenname);
		md().wait(5);

		return p_name;
	}

	public String changePassword()
	{
		md().waitForPresence(elements().btnChangePassword, "Button change password doesn't appear");
		md().click(elements().btnChangePassword);
		md().waitForPresence(elements().inputFieldNewPassword, "Input field new password doesn't appear");
		md().typeAndClear(elements().inputFieldNewPassword, manager.testUser().getPassword() + "Q");
		md().typeAndClear(elements().inputFieldRetypeNewPassword, manager.testUser().getPassword() + "Q");
		String p_newPassword = manager.testUser().getPassword() + "Q";
		md().click(elements().btnSubmitPassword);
		md().wait(5);

		return p_newPassword;
	}

	public void checkTermsAndConditions()
	{
		md().click(elements().btnTermsAndConditions);
		md().wait(5);
		md().waitForVisibility(elements().blockTermsAndConditions, "Terms and conditions block doesn't appear");
		md().waitForVisibility(elements().btnSubmitTermsAndConditions, "Terms and conditions submit button doesn't appear");
	}

	private Object getAttribute(By p_element)
	{
		WebElement p_wink = manager.wd().driver().findElement(p_element);
		Object attribute = p_wink.getAttribute("checked");

		return attribute;
	}

	public void setActivityNotifications()
	{
		md().waitForPresence(elements().btnActivityNotifications, "Activity Notifications button doesn't appear");
		md().click(elements().btnActivityNotifications);
		md().waitForPresence(elements().chbxWinks, "Checkbox wink doesn't appear");
		if (this.getAttribute(elements().chbxWinks).equals("false"))
		{
			log().errorAndScreen("Checkbox Wink doesn't marked as true");
		}
		if (this.getAttribute(elements().chbxNewMessage).equals("false"))
		{
			log().errorAndScreen("Checkbox new message doesn't marked as true");
		}
		if (this.getAttribute(elements().chbxVisitors).equals("false"))
		{
			log().errorAndScreen("Checkbox new message doesn't marked as true");
		}
		md().click(elements().chbxWinks);
		md().click(elements().chbxNewMessage);
		md().click(elements().chbxVisitors);

	}

	public void checkActivityNotifications()
	{
		md().waitForPresence(elements().btnActivityNotifications, "Activity Notifications button doesn't appear");
		md().click(elements().btnActivityNotifications);
		md().waitForPresence(elements().chbxWinks, "Checkbox wink doesn't appear");
		if (this.getAttribute(elements().chbxWinks).equals("true"))
		{
			log().errorAndScreen("Checkbox Wink doesn't marked as false");
		}
		if (this.getAttribute(elements().chbxNewMessage).equals("true"))
		{
			log().errorAndScreen("Checkbox new message doesn't marked as false");
		}
		if (this.getAttribute(elements().chbxVisitors).equals("true"))
		{
			log().errorAndScreen("Checkbox new message doesn't marked as false");
		}
	}

	public void setEmailNotifications()
	{
		md().waitForPresence(elements().btnEmailNotifications, "Email Notifications button doesn't appear");
		md().click(elements().btnEmailNotifications);
		md().waitForPresence(elements().btnEmailNotifications, "Checkbox email doesn't appear");
		if (this.getAttribute(elements().chbxPersonalAlerts).equals("false"))
		{
			log().errorAndScreen("Checkbox personal alerts doesn't marked as true");
		}
		if (this.getAttribute(elements().chbxSiteOffers).equals("false"))
		{
			log().errorAndScreen("Checkbox site offers alerts doesn't marked as true");
		}

		if (this.getAttribute(elements().chbxAdminAllerts).equals("false"))
		{
			log().errorAndScreen("Checkbox admin allerts doesn't marked as true");
		}
		md().click(elements().chbxPersonalAlerts);
		md().click(elements().chbxSiteOffers);
		md().click(elements().chbxAdminAllerts);

	}

	public void checkEmailNotifications()
	{
		md().waitForPresence(elements().btnEmailNotifications, "Email Notifications button doesn't appear");
		md().click(elements().btnEmailNotifications);
		md().waitForPresence(elements().btnEmailNotifications, "Checkbox email doesn't appear");
		if (this.getAttribute(elements().chbxPersonalAlerts).equals("true"))
		{
			log().errorAndScreen("Checkbox personal alerts doesn't marked as false");
		}
		if (this.getAttribute(elements().chbxSiteOffers).equals("true"))
		{
			log().errorAndScreen("Checkbox site offers alerts doesn't marked as false");
		}

		if (this.getAttribute(elements().chbxAdminAllerts).equals("true"))
		{
			log().errorAndScreen("Checkbox admin allerts doesn't marked as false");
		}
	}

	public void validateScreenname(String p_nameUniquePrefix)
	{
		md().click(elements().btnChangeScreenname);
		md().waitForPresence(elements().inputFieldChangeScreenname, "Input field change screenname doesn't appear");
		String p_screenname = md().getText(elements().inputFieldChangeScreenname);
		if (p_screenname.equals(p_name + p_nameUniquePrefix))
		{
			manager.testUser().setScreenname(p_screenname);
			manager.db().updateUserData(manager.testUser());
		}
		else
		{
			log().warn(
					"Screename doesn't update correctly: Actual result => " + p_screenname + " Saved result => "
							+ p_name + p_nameUniquePrefix);
		}
	}
}
